package com.newsmanagement.service.impl;

import com.newsmanagement.common.Constants;
import com.newsmanagement.common.Page;
import com.newsmanagement.dao.INewsContentServiceDao;
import com.newsmanagement.entity.NewsCategoryEntity;
import com.newsmanagement.entity.NewsContentEntity;
import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.service.INewsContentService;
import com.newsmanagement.util.CheckAndChangeUtil;
import com.newsmanagement.util.EntityMapChange;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xbr
 * 2017/12/4 16:29
 * 新闻分类管理service实现
 */
@Service
@Transactional
public class NewsContentServiceImpl implements INewsContentService {


    @Autowired
    private INewsContentServiceDao newsContentServiceDao;


    /**
     * （后台）添加新闻内容
     * @param contentTitle 标题
     * @param contentTxt 内容
     * @param categoryId 分类id
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages addNewsContent(String contentTitle ,String contentTxt ,Integer categoryId){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证新闻标题
        if(!CheckAndChangeUtil.checkStringHaveData(contentTitle)){
            contentTitle = "";
        }

        //验证新闻内容
        if(!CheckAndChangeUtil.checkStringHaveData(contentTxt)){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("请编辑新闻内容");
            return returnedMessages;
        }

        //验证新闻内容
        if(null == categoryId || categoryId < Constants.INT_ONE){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("请选择新闻分类");
            return returnedMessages;
        }

        //添加新闻内容信息
        newsContentServiceDao.addNewsContent(contentTitle ,contentTxt ,categoryId);

        return returnedMessages;
    }

    /**
     * （后台）添加新闻内容
     * @param newsContent 新闻内容
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages updateNewsContent(NewsContentEntity newsContent){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证新闻内容实体
        if(null == newsContent){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("未接收到新闻内容");
            return returnedMessages;
        }

        //验证新闻标题
        if(!CheckAndChangeUtil.checkStringHaveData(newsContent.getContentTitle())){
            newsContent.setContentTitle("");
        }

        //验证新闻内容
        if(!CheckAndChangeUtil.checkStringHaveData(newsContent.getContentTxt())){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("请编辑新闻内容");
            return returnedMessages;
        }

        //验证新闻内容
        if(null == newsContent.getCategoryId() || newsContent.getCategoryId() < Constants.INT_ONE){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("请选择新闻分类");
            return returnedMessages;
        }

        //修改新闻内容信息
        int addNumber = newsContentServiceDao.updateNewsContent(
                newsContent.getContentId() ,
                newsContent.getContentTitle() ,
                newsContent.getContentTxt() ,
                newsContent.getCategoryId()
        );

        //执行结果验证
        if (addNumber < Constants.INT_ONE){
            returnedMessages.setCode(400);
            returnedMessages.setMessage("新闻内容更新未成功");
        }

        return returnedMessages;
    }

    /**
     * （后台）删除新闻内容(逻辑删除)
     * @param contentId 新闻内容id
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages deleteNewsContent(Integer contentId){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证新闻内容实体
        if(null == contentId || contentId < Constants.INT_ONE){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("新闻内容主键为空");
            return returnedMessages;
        }

        //删除新闻内容
        newsContentServiceDao.deleteNewsContent(contentId);

        return returnedMessages;
    }

    /**
     * （前台）根据新闻相关条件查询对应的新闻内容信息
     * 几个条件不可相互存在
     * @param contentId 新闻内容id
     * @param keyWords 关键字模糊查询
     * @param categoryId 新闻分类id
     * @param selectValue 条件选择标记：1，新闻内容id；2，关键字模糊查询；3：新闻分类id  默认为1
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages selectContent(Integer contentId ,String keyWords ,Integer categoryId ,Integer selectValue){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证标记
        if (null == selectValue || selectValue > Constants.INT_THREE || selectValue < Constants.INT_ONE){
            selectValue = Constants.INT_ONE;
        }

        List<NewsContentEntity> newsContentList = new ArrayList<NewsContentEntity>();
        List<Map<String ,Object>> listMap = null;
        switch (selectValue){
            //新闻内容id
            case Constants.INT_ONE:
                NewsContentEntity newsContent = EntityMapChange.mapToEntity(newsContentServiceDao.selectNewsContentByContentId(contentId) ,NewsContentEntity.class);
                if (null == newsContent){
                    returnedMessages.setCode(400);
                    returnedMessages.setMessage("根据新闻内容id未查询到对应的新闻内容详情");
                }else {
                    returnedMessages.setData(newsContent);
                }
                break;
            //关键字模糊查询
            case Constants.INT_TWO:
                listMap = newsContentServiceDao.selectNewsContentListByKeyword(keyWords);

                if (null != listMap && listMap.size() > Constants.INT_ZERO && null != listMap.get(Constants.INT_ZERO)){
                    for (int i = 0; i < listMap.size(); i++) {
                        if (null != listMap.get(i)){
                            newsContentList.add(EntityMapChange.mapToEntity(listMap.get(i) ,NewsContentEntity.class));
                        }

                    }
                    returnedMessages.setData(newsContentList);
                }else {
                    returnedMessages.setCode(400);
                    returnedMessages.setMessage("根据关键字模糊查询未查询到对应的新闻内容信息");
                }
                break;
            //新闻分类id
            case Constants.INT_THREE:
                listMap = newsContentServiceDao.selectNewsContentListByCategoryId(categoryId);
                if (null != listMap && listMap.size() > Constants.INT_ZERO && null != listMap.get(Constants.INT_ZERO)){
                    for (int i = 0; i < listMap.size(); i++) {
                        if (null != listMap.get(i)){
                            newsContentList.add(EntityMapChange.mapToEntity(listMap.get(i) ,NewsContentEntity.class));
                        }

                    }
                    returnedMessages.setData(newsContentList);
                }else {
                    returnedMessages.setCode(400);
                    returnedMessages.setMessage("根据新闻分类id未查询到对应的新闻内容信息");
                }
                break;
            default:
                returnedMessages.setCode(500);
                returnedMessages.setMessage("条件错误，未选中使用的条件");
                break;
        }

        return returnedMessages;
    }


    /**
     * 分页查询新闻列表
     * @param jumpPage 跳转页面
     * @param showSize 每页显示的数据条数
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages newsContentList(Integer jumpPage ,Integer showSize){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        int totalCount = newsContentServiceDao.getNewsContentListCount();

        List<Map<String ,Object>> newsContentList =  newsContentServiceDao.getNewsContentList();

        Page page = new Page(totalCount,jumpPage,showSize);

        newsContentList = newsContentList.subList(page.getStartPage(),page.getEndPage());

        List list = new ArrayList();
        if (null != newsContentList){
            for (int i = 0; i < newsContentList.size(); i++) {
                list.add(EntityMapChange.mapToEntity(newsContentList.get(i) ,NewsContentEntity.class));
            }

            page.setList(list);
            returnedMessages.setData(page);
            return returnedMessages;
        }else{
            returnedMessages.setCode(400);
            returnedMessages.setMessage("未查找到对应信息");
            return returnedMessages;
        }
    }
}
